from PIL import Image

def image_to_ascii(image_path="first.jpg", output_width=100):
    # 定义字符集（按密度降序排列）
    ascii_chars = "@%#*+=-:. "
    img = Image.open("first.jpg")
    # 1.resize
    width, height = img.size
    ratio = width / height
    output_height = int(output_width / ratio)
    resized_image = img.resize((output_width,output_height))
    # 2.convert
    gray_image = resized_image.convert('L')
    print(gray_image)
    # 3.output
    pixels = gray_image.getdata()
    for pixel in pixels:
        print(pixel)
    characters = "".join([ascii_chars[int(pixel // 25)-1] for pixel in pixels])
    seq = [characters[i:i + output_width] for i in range(0, len(characters), output_width)]
    output = "\n".join(seq)
    print(output)
image_to_ascii()